var layer = layui.layer
    ,table = layui.table
    ,$ = layui.jquery,
    form=layui.form,
    formtype = '',//0为新增，1为修改
    dl=getQueryVariable("dl"),
    xl=getQueryVariable("xl")
;
var pk=0;
if(dl==""){
    dl=2;
}
if(xl==""){
    xl=6;
}
var data1 = [{'id':'1','portcode':'text1','chinesename':'text2','englishname':'text3','country':'text4','describe':'text5'},{'id':'2','portcode':'text1','chinesename':'text2','englishname':'text3','country':'text4','describe':'text5'}];
table.render({
    elem: '#portid'
    ,type:"GET"
    ,url:"dictionary2/getValueByPage"
    ,where:{"type":dl,"lx":xl,"name":JSON.stringify($('#names').val())}
    ,toolbar: '#toolbarDemo'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field:'code', title:'港口编码', fixed: true}
        ,{field:'englishname', title:'英文名称', fixed: true}
        ,{field:'name', title:'中文名称', fixed: true}
        ,{field:'country', title:'所属国家', fixed: true}
        ,{field:'remark', title:'描述', fixed: true}

    ]]
    ,id: 'porttableid'
    ,height: $(document).height() - $('#portid').offset().top - 50
    ,page: true
});
table.on('toolbar(porttable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
        case 'newport':
            layer.open({
                type: 1,
                title: "添加港口",
                area: ['685px', '435px'],
                content: $("#fromeditor")//引用的弹出层的页面层的方式加载修改界面表单
            });
            //清空表单
            $('#editor-form')[0].reset();
            form.render();
            $("input[name='portcode']").removeAttr("readonly");
            formtype = 0;
            pk=0;
            break;
        case 'edit':
            if(checkStatus.data.length==0){
                layer.msg("请选择一条数据！");
                return false;
            }
            layer.open({
                type: 1,
                title: "修改港口",
                area: ['685px', '435px'],
                content: $("#fromeditor")//引用的弹出层的页面层的方式加载修改界面表单
            });
            //给表单赋值
            form.val("editor-form",{
                "code":checkStatus.data[0].code,
                "name":checkStatus.data[0].name,
                "remark":checkStatus.data[0].remark,
                "englishname":checkStatus.data[0].englishname,
                "country":checkStatus.data[0].country
            })
            $("input[name='code']").attr("readonly","true");
            formtype = 1;
            pk=checkStatus.data[0].id;
            break
        case 'deleteport':
            if(checkStatus.data.length==0){
                layer.msg("请选择一条数据！");
                return false;
            }
            var data = checkStatus.data;
            var delid = [];
            for (var i = 0;i<data.length;i++){
                delid.push(data[i].id)
            }
           /* layer.msg('需要删除的数据ID为：'+ JSON.stringify(delid) + ',执行删除');
            table.reload(); //ajax删除成功之后刷新表格*/
            if(del(delid)){
                layer.msg("删除成功");
                table.reload('porttableid', {
                    url:"dictionary2/getValueByPage",
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where:{"type":dl,"lx":xl,"name":$('#names').val()}
                }, 'data');
            }else{
                layer.msg("删除失败");
            }
            break;
        case 'searchs':
            var names = $('#names').val();
            table.reload('porttableid', {
                url:"dictionary2/getValueByPage",
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where:{"type":dl,"lx":xl,"name":$('#names').val()}
            }, 'data');
            break;
    };
});

form.on('submit(submitform)', function(data) {
   /* layer.closeAll();
    layer.msg("提交成功", {icon: 6});
    if(formtype==0)  layer.msg("新增操作"+JSON.stringify(data.field));
    else layer.msg("修改操作"+JSON.stringify(data.field));*/
    if(insert(data)){
        layer.msg("保存成功", {icon: 6});
        table.reload('porttableid', {
            url:"dictionary2/getValueByPage",
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where:{"type":dl,"lx":xl,"name":JSON.stringify($('#names').val())}
        }, 'data');
        layer.closeAll(); //执行重载
    }else{
        layer.msg("保存失败", {icon: 6});
    }
    return false;
})
function del(ids) {
    var flag=false;
    $.ajax({
        type : 'GET',
        //  url : 'resource/getTree',
        data: {"ids":JSON.stringify(ids)},
        url:'dictionary2/deleteByPrimaryKey',
        dataType:"json",
        //contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                flag=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}
function insert(data) {
    var flag=false;
    data.field.formtype=formtype;
    data.field.type=dl;
    data.field.lx=xl;
    if(formtype ==1){
        data.field.id=pk;
    }else if(data.field.code==undefined){
        data.field.code=xl;
    }
    $.ajax({
        type : 'POST',
        //  url : 'resource/getTree',
        data: JSON.stringify(data.field),
        url:'dictionary2/insertandupdate',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                flag=true;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return flag;
}